#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <rte_eal.h>
#include <rte_ethdev.h>
#include <rte_mbuf.h>

#define NUM_MBUFS (8191)
#define MBUF_CACHE_SIZE (256)
#define RX_RING_SIZE (128)
#define TX_RING_SIZE (512)


static struct rte_mempool * g_pool;

static void l2_forward(struct rte_mbuf * mbuf, uint16_t port_id){
    rte_eth_tx_brust(port_id, 0, &mbuf, 1);
}

static void process_packets(uint16_t port_id){
    struct rte_mbuf * buf[32];
    uint16_t num_rx;
    num_rx = rte_eth_rx_brust(port_id, 0, buf, 32);
    for(int i = 0; i < num_rx; i++){
        l2_forward(buf[i], port_id);
    }
}

int main(int argc, char * argv[]){
    int ret = 0;
    if(rte_eal_init(argc, argv) < 0){
        rte_exit(EXIT_FAILURE, "---->  ");
    }
    g_pool = rte_pktmbuf_pool_create("",NUM_MBUFS, MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
    if(!g_pool){
        rte_exit(EXIT_FAILURE,"---> ");
    }
    uint16_t port_id = 0;
    struct rte_eth_dev_info dev_inf;
    ret = rte_eth_dev_info_get(port_id, &dev_info);
    if(ret != 0){
        
    }
    struct ether_addr addr;
    ret = rte_eth_macaddr_get(port_id, &addr);
    printf("Port %u MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
                                                            port_id,
                                                            addr.addr_bytes[0],
                                                            addr.addr_bytes[1],
                                                            addr.addr_bytes[2],
                                                            addr.addr_bytes[3],
                                                            addr.addr_bytes[4],
                                                            addr.addr_bytes[5]);

    if(rte_eth_dev_start(port_id) < 0){
        rte_exit(EXIT_FAILURE,"---> ");
    }
    
    while(1){
        process_packets(port_id);
    }                                                            
    
    return 0;
}